Thread: [DEBATE]int main VS void main?

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main.
    Eh? Intent means nothing here. The standard requires int main. Who cares what you intend to do?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by User Name:
    If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main.
    The funny thing is that if you never intend to explicitly return from the global main function, int main is just fine. The use of void main, on the other hand, may result in a compile error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Memloop View Post
    The point is that you can use void main and still be compliant if you're using a freestanding implementation that supports it. You're not even required to have a main function to begin with if your compiler supports it. It's not going to be portable, but that's not always a problem.
    No question that, if your compiler supports void main(), you are allowed to use it.

    Also correct that a compiler which supports entry points other than int main() might be properly characterised as a freestanding implementation. There is also nothing in the standard preventing an implementation from being both freestanding and hosted - the difference between those two terms comes down to the minimum set of libraries that must be supported, not to the extensions supported.

    However, an implementation (compiler and library) that supports void main() is only compliant (or, actually, conformant) if it also supports int main().

    There is no actual definition in the standard of what compliant means for code (or for a programmer).

    If you mean that code which uses void main() is well-formed (the C++ standard defines a well-formed program to be "a C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule") then you are potentially correct, whether an implementation is freestanding or hosted.

    Although usage of void main() is likely to be diagnosed - by either the compiler or the linker - if the compiler does not specifically support void main().
    Last edited by grumpy; 11-26-2010 at 03:40 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    However, an implementation (compiler and library) that supports void main() is only compliant (or, actually, conformant) if it also supports int main().
    That is an interesting observation. I assumed that "in a freestanding environment, start-up and termination is implementation-defined" meant that, like in C, "in a freestanding environment, the name and type of the function called at program startup are implementation-defined", but the statement that "all implementations shall allow both of the following definitions of main" clearly contradicts such an assumption.

    What does "start-up and termination is implementation-defined" really mean, then? All that is offered as elaboration is the statement that "startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Unless there is an argument here on why to use "void main" I don't think there is a real debate.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    That is an interesting observation. I assumed that "in a freestanding environment, start-up and termination is implementation-defined" meant that, like in C, "in a freestanding environment, the name and type of the function called at program startup are implementation-defined", but the statement that "all implementations shall allow both of the following definitions of main" clearly contradicts such an assumption.
    Yep. My guess is that the standards committee had a philosophy of allowing as much freedom as possible, while having to specify (at least) a minimum set of requirements that an implementation must meet. Those types of goals imply some contradiction or trade-off.

    Quote Originally Posted by laserlight View Post
    What does "start-up and termination is implementation-defined" really mean, then? All that is offered as elaboration is the statement that "startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration".
    My interpretation is that startup is the process the implementation orchestrates before the first statement in main() (or any other entry point). That includes initialising standard I/O streams (stdout/cout, stdin/cin, etc), the process of ensuring constructors of objects with static storage duration are executed, and the process of preparing to call (and then calling) main().

    Similarly, I would interpret termination as the process that the implementation orchestrates after main() returns (capturing the return value from main() so it can be returned to the host environment, invoking destructors of objects with static storage duration, closing standard I/O streams, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  5. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM